package com.pg_chat.utils;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class ContentCatcher {
    /**
     * get the content from the file
     * @param path file path
     * @return
     */
    public String getContent(String path){
        FileInputStream fIS = null;
        BufferedReader buffer= null;
        StringBuffer result = new StringBuffer();
        String line= "";
        try {
            fIS = new FileInputStream(path);
            buffer = new BufferedReader(new InputStreamReader(fIS,"UTF-8"));
            while ((line = buffer.readLine())!=null ){
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {

            try {
                buffer.close();
                fIS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }


    public String content4Classes(String name){
        String result = "";
        //FileInputStream fileIS = (FileInputStream) ContentCatcher.class.getResourceAsStream(name);
        System.out.println(ContentCatcher.class.getResource(name));


        return result;
    }

    public static void main(String[] args) {
        ContentCatcher cc = new ContentCatcher();
        cc.content4Classes("/wordForbidden/ban.txt");
    }

}
